package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.dao.domain.UserBlackList;
import com.yunkeji.dao.mapper.UserBlackListMapper;
import com.yunkeji.dao.vo.UserBlackListVo;
import com.yunkeji.system.domain.bo.UserBlackListBo;
import com.yunkeji.system.service.IUserBlackListService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 礼物配置信息Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class UserBlackListServiceImpl implements IUserBlackListService {

    private final UserBlackListMapper baseMapper;

    /** 查询礼物配置信息 */
    @Override
    public UserBlackListVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询礼物配置信息列表 */
    @Override
    public TableDataInfo<UserBlackListVo> queryPageList(UserBlackListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserBlackList> lqw = buildQueryWrapper(bo);
        Page<UserBlackListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询礼物配置信息列表 */
    @Override
    public List<UserBlackListVo> queryList(UserBlackListBo bo) {
        LambdaQueryWrapper<UserBlackList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserBlackList> buildQueryWrapper(UserBlackListBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserBlackList> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFromUserId() != null, UserBlackList::getFromUserId, bo.getFromUserId());
        lqw.eq(bo.getToUserId() != null, UserBlackList::getToUserId, bo.getToUserId());
        return lqw;
    }

    /** 新增礼物配置信息 */
    @Override
    public Boolean insertByBo(UserBlackListBo bo) {
        UserBlackList add = BeanUtil.toBean(bo, UserBlackList.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            //    bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改礼物配置信息 */
    @Override
    public Boolean updateByBo(UserBlackListBo bo) {
        UserBlackList update = BeanUtil.toBean(bo, UserBlackList.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(UserBlackList entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除礼物配置信息 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
